/*测试数据*/
var testdata={
	id: 1,
	supplierName: '李波',
    supplierPhone: '18817802281',
    supplierAddress: '上海市宝山区同济支路',
	createTime: 1502937009
};

$(function(){
	getDataMes();
	$("#btnSaveEdit").on('click',function(){
		submitData();
	});
	$("#btnReback").on('click',function(){
		var tgid=getUrlParam("tgid");
		if(tgid!=null && tgid>0){
			history.go(-1);
		}else{
			$("#editForm")[0].reset();
		}
	});
});
/*修改数据时，获取数据并显示*/
function getDataMes(){
	var tgid=getUrlParam("tgid");
	if(tgid!=null && tgid>0){
		
		/*if(testdata.id!=null){
			$("#manageContent").attr({"tgid":testdata.id});
		}
		if(testdata.supplierName!=null){
			$("#supplierName").val(testdata.supplierName);
		}
        if(testdata.supplierPhone!=null){
            $("#supplierPhone").val(testdata.supplierPhone);
        }
        if(testdata.supplierAddress!=null){
            $("#supplierAddress").val(testdata.supplierAddress);
        }*/
		$.post(
            gobalServer+"supplier/selectOne",
            {"id":tgid},
            function(backData){
                if(backData.statusCode==200){
                    var datarow=backData.object;
                    if(datarow.id!=null){
                        $("#manageContent").attr({"tgid":datarow.id});
                    }
                    if(datarow.supplierName!=null){
                        $("#supplierName").val(datarow.supplierName);
                    }
                    if(datarow.supplierPhone!=null){
                        $("#supplierPhone").val(datarow.supplierPhone);
                    }
                    if(datarow.supplierAddress!=null){
                        $("#supplierAddress").val(datarow.supplierAddress);
                    }
                }else{
                    consoleMes($("#manageContent"),3000,"warning",backData.msg);//success、info、warning、danger
                }
            }
		);
	}
};
/*新增或修改数据*/
function submitData(){
	var tgid=$("#manageContent").attr("tgid");
	var supplierName=$("#supplierName").val().trim();
    var supplierPhone=$("#supplierPhone").val().trim();
    var supplierAddress=$("#supplierAddress").val().trim();
	if(supplierName==null || supplierName==""){
		consoleMes($("#manageContent"),2000,"warning","供应商名称不能为空！");
		return;
	}
    if(supplierPhone==null || supplierPhone==""){
        consoleMes($("#manageContent"),2000,"warning","供应商电话不能为空！");
        return;
    }
    if(!ischeckmobile(supplierPhone)){
        consoleMes($("#manageContent"),2000,"warning","手机格式不正确！");
        return;
    }
    if(supplierAddress==null || supplierAddress==""){
        consoleMes($("#manageContent"),2000,"warning","供应商地址不能为空！");
        return;
    }
    /*if(tgid!=null && tgid>0){
        consoleMes($("#manageContent"),2000,"success","数据修改成功，正在刷新！");
    }else{
        consoleMes($("#manageContent"),2000,"success","添加数据成功，正在跳转！");//success、info、warning、danger
	}*/

	if(tgid!=null && tgid>0){
		if(getUrlParam("tgid")!=null){
			/*consoleMes($("#manageContent"),2000,"success","数据修改成功，正在刷新！");//success、info、warning、danger
        	setTimeout(function () {
		    	window.location.reload();
		    }, 2500);*/
            $.post(
                gobalServer+"supplier/update",
                {"id":tgid,"supplierName":supplierName,"supplierPhone":supplierPhone,"supplierAddress":supplierAddress},
                function(backData){
                    if(backData.statusCode==200){
                        consoleMes($("#manageContent"),2000,"success","数据修改成功，正在刷新！");//success、info、warning、danger
                        setTimeout(function () {
                            window.location.reload();
                        }, 2500);
                    }else{
                        consoleMes($("#manageContent"),3000,"warning",backData.msg);//success、info、warning、danger
                    }
                }
            );
	    }else{
			consoleMes($("#manageContent"),2000,"warning","无法提交，请刷新页面重试！");
		}
	}else{
		/*consoleMes($("#manageContent"),2000,"success","添加数据成功，正在跳转！");//success、info、warning、danger
    	setTimeout(function () {
	    	window.location.href=gobalWeb+"product/brandshow.html";
	    }, 2500);*/
        $.post(
            gobalServer+"supplier/insert",
            {"supplierName":supplierName,"supplierPhone":supplierPhone,"supplierAddress":supplierAddress},
            function(backData){
                if(backData.statusCode==200){
                    consoleMes($("#manageContent"),2000,"success","添加数据成功，正在跳转！");//success、info、warning、danger
                    setTimeout(function () {
                        window.location.href=gobalWeb+"product/suppliershow.html";
                    }, 2500);
                }else{
                    consoleMes($("#manageContent"),3000,"warning",backData.msg);//success、info、warning、danger
                }
            }
        );

	}
		
};
